package com.owc.controller.app;

import com.owc.common.custom.annotation.Advice;
import com.owc.domain.BaseResult;
import com.owc.domain.PageVo;
import com.owc.domain.dto.*;
import com.owc.domain.vo.app.BuyProductVo;
import com.owc.domain.vo.app.ProductDetailVo;
import com.owc.filter.content.SecurityContextHolder;
import com.owc.service.app.InvestmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author xiaom
 * @Date 2020/2/24 13:54
 * @Version 1.0.0
 * @Description <>
 **/
@RestController
@RequestMapping("/investment")
@Api(tags = "(APP-V2)投资管理")
public class InvestmentController {

    private final InvestmentService investmentService;

    public InvestmentController(InvestmentService investmentService) {
        this.investmentService = investmentService;
    }

    @PostMapping(value = "/list")
    @ApiOperation("获取投资产品列表")
    public BaseResult<List<ProductDTO>> productList(@RequestBody PageVo pageVo){
        return investmentService.productList(pageVo.getPageIndex(),pageVo.getPageSize());
    }

    @PostMapping(value = "/detail")
    @ApiOperation("获取投资产品详情")
    public BaseResult<ProductDetailDTO> detail(@RequestBody @Valid ProductDetailVo productDetailVo){
        return BaseResult.success(investmentService.getById(productDetailVo.getId()));
    }

    @PostMapping(value = "/buyDetail")
    @ApiOperation("购买详情")
    public BaseResult<ProductBuyDTO> buyDetail(@RequestBody @Valid ProductDetailVo productDetailVo){
        return BaseResult.success(investmentService.buyDetail(productDetailVo.getId()));
    }

    @PostMapping(value = "/buyProduct")
    @ApiOperation("购买产品")
    public BaseResult<BuyProductDTO> buyProduct(@RequestBody @Valid BuyProductVo buyProductVo) throws Exception {
        buyProductVo.setUserId(SecurityContextHolder.getCurrentUserId());
        return BaseResult.success(investmentService.buyProduct(buyProductVo));
    }

}
